﻿/*
 * Created by SharpDevelop.
 * User: lebeda
 * Date: 20.2.2014
 * Time: 21:22
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;

namespace simpleExplorer
{
    /// <summary>
    /// Description of MenuItemProviderTools.
    /// </summary>
    public class Tools
    {
        public const string FS_SEPARATOR = "\\";
        
        private Tools() {}
        
        public static MenuItemProvider menuProviderFactory(MenuItem menuItem)
        {
            MenuItemProvider result;
            
            switch(menuItem.itemType) {
                case ItemType.filesystemMenuProvider:
                    result = new menuProviderImpl.FilesystemMenuProvider(menuItem);
                    break;
                case ItemType.noteMenuProvider:
                    result = new menuProviderImpl.NotesMenuProvider(menuItem);
                    break;
                case ItemType.xmlMenuProvider:
                    result = new menuProviderImpl.XmlMenuProvider(menuItem);
                    break;
                case ItemType.fsRecurciveMenuProvider:
                    result = new menuProviderImpl.FilesystemRecursiveMenuProvider(menuItem);
                    break;
                default:
                    throw new InvalidDataException("Invalid type of menu provider: " + menuItem.itemType);
            }
            
            return result;
        }
        
        
        public static string configDirName()
        {
            String personalFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            personalFolder += FS_SEPARATOR + "simpleExplorer";
            
            if (!Directory.Exists(personalFolder)) {
                Directory.CreateDirectory(personalFolder);
            }
            
            return personalFolder;
        }
        
        public static string replaceConfig(string src, MenuItem parent) {
            string result = src;
            
            if (!String.IsNullOrWhiteSpace(src)) {
                result = result.Replace("%CONFIGDIR%", Tools.configDirName());
                
                result = result.Replace("%CAPTION%", parent.caption);
                result = result.Replace("%PARAM%", parent.param);
                result = result.Replace("%CMD%", parent.cmd);
                result = result.Replace("%DIR%", parent.directory);
                
                if (File.Exists(parent.cmd)) {
                    result = result.Replace("%EXT%", Path.GetExtension(parent.cmd));
                    result = result.Replace("%FNAME%", Path.GetFileNameWithoutExtension(parent.cmd));
                }
            }
            
            return result;
        }
        
        
        public static void expandMacros(MenuItem lMenuItem, MenuItem parentMenuItem)
        {
            if (lMenuItem == null)
                throw new ArgumentNullException("lMenuItem");
            if (parentMenuItem == null)
                throw new ArgumentNullException("ParentMenuItem");
            
            
            lMenuItem.caption = Tools.replaceConfig(lMenuItem.caption, parentMenuItem);
            lMenuItem.cmd = Tools.replaceConfig(lMenuItem.cmd, parentMenuItem);
            lMenuItem.directory = Tools.replaceConfig(lMenuItem.directory, parentMenuItem);
            lMenuItem.param = Tools.replaceConfig(lMenuItem.param, parentMenuItem);
        }
        
        public static MenuItem getMenuUp(MenuItemProvider menuProvider)
        {
            MenuItem lMenu = menuProvider.GetSelfMenuCreateDefinition().menuUp;
            if (lMenu == null)
                lMenu = menuProvider.GetSelfMenuCreateDefinition();
            
            return lMenu;
            
        }
    }
}
